﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuGanDong : ControllerBase
{
    private bool keepTurning = true;
    private bool keepMoving = true;
    private float turnDir = 1;
    private Vector2 enemyPos;

    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "AE86",
            Author = "ShiKi",
            BaseImage = "Sprite/Base/Base09",
            GunImage = "Sprite/Gun/Gun06"
        };
    }

    public override IEnumerator LoopAction()
    {
        while (true)
        {
            if (keepTurning)
            {
                //Debug.Log("ffffff");
                Turn(Time.realtimeSinceStartup * 100, 100);
            }
            if (keepMoving)
            {
                Move(1, Time.realtimeSinceStartup * 80, 0.1f);
            }
            yield return new WaitForSeconds(0.1f);
        }
    }

    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        if (scanOthersData.IsEnemy)
        {
            enemyPos = scanOthersData.othersPosition;
            //Debug.Log(GetAngle(scanOthersData.othersSpeed));
            StopTurn(); Stop();
            CoroutineMono.StartCoroutine(StopTurningFor(4f));
            //CoroutineMono.StartCoroutine(StartSmallRangeTurning(5f));
            CoroutineMono.StartCoroutine(StopMovingFor(0.2f));
            Move(1, GetAngle(scanOthersData.othersSpeed), 2f);
            //Debug.Log("GetAngle:"+GetAngle(scanOthersData.othersSpeed) + "   Speed:" + scanOthersData.othersSpeed);
            if (Vector2.Distance(Data.currentPosition, enemyPos) < 3 || Mathf.Abs(scanOthersData.othersAngle - Data.currentAngle) < 90)
            {

                Fire(2, 5);
            }
            else
            {
                float angleToTurn = Data.currentAngle + 15 / Vector2.Distance(Data.currentPosition, enemyPos);
                //angleToTurn = 3;
                turnDir = 1;
                Debug.Log("当前角度：" + Data.currentAngle + "      " + "需要转的角度：" + (Data.currentAngle + angleToTurn));
                Turn(angleToTurn, 100 * turnDir);
                Fire(1, 5);
            }
        }
    }

    public override void OnScaned(float angle)
    {
        Stop();
        CoroutineMono.StartCoroutine(StopMovingFor(1f));

        //Move(1, angle+90, 2f);
    }

    public override void OnCrashWall()
    {
        Stop();
        CoroutineMono.StartCoroutine(StopMovingFor(2f));
        MoveTo(2, Vector3.zero);
    }

    public override void Start()
    {
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {

    }

    public override void OnHitted(float bulletAngle)
    {

    }

    private IEnumerator StopTurningFor(float duration)
    {
        keepTurning = false;
        yield return new WaitForSeconds(duration);
        keepTurning = true;
    }
    private IEnumerator StopMovingFor(float duration)
    {
        keepMoving = false;
        yield return new WaitForSeconds(duration);
        keepMoving = true;
    }
    private IEnumerator StartSmallRangeTurning(float duration)
    {
        Turn(10, 100);
        yield return new WaitForSeconds(1);
        Turn(-10, 100);
        yield return new WaitForSeconds(1);
        Turn(10, 100);
        yield return new WaitForSeconds(1);
        yield return new WaitForSeconds(duration);

    }


    private float GetAngle(Vector2 speed)
    {
        float angle = 0;
        if (speed.y <= 0)
        {
            angle = Vector2.Angle(Vector2.right, speed);
        }
        else
        {
            angle = 360 - Vector2.Angle(Vector2.right, speed);
        }
        return angle;
    }
}
